AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include('shared.lua')

function ENT:Initialize()
	self.Entity:SetModel("models/props_junk/TrashBin01a.mdl")
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)
	local phys = self.Entity:GetPhysicsObject()
	if(phys:IsValid()) then phys:Wake() end
	self.Entity:SetNWBool("sparking",false)
	self.Entity:SetNWInt("damage",self.Damage)
	self.Entity:SetNWInt("upgrade", 0)
	self.LastUsed = CurTime()
	self.Timelefts = 300
	self.Ready = true
	self.Guns = {}
	self.Models = {}
end

function ENT:Use(activator,caller)
	if (self.LastUsed+0.5>CurTime()) then
		self.LastUsed = CurTime()
	else
		self.LastUsed = CurTime()
		local ply = self.Entity:GetNWEntity("owner")
		if(self.Guns[1] == "") then count = 0 else count = table.Count(self.Guns) end
			if (activator==ply || activator:IsAllied(ply)) then
				self.Locked = true
			end
			// to prevent having duplicates of it open.
			umsg.Start("killgunrecyclergui", activator)
				umsg.Short(self.Entity:EntIndex())
			umsg.End()
			umsg.Start("msggunrecycler", activator)
				umsg.Short(count )
				umsg.Short(self.Entity:EntIndex())
				umsg.Bool(self.Ready)
				umsg.Short(self.Timelefts)
			umsg.End()
	end
end

function ENT:Think()
	if (ValidEntity(self.Entity:GetNWEntity("owner"))==false) then
		self.Entity:Remove()
	end
end

function ENT:DropAllGuns(ply)
	for i=1, table.Count(self.Guns), 1 do
		local gunt = self.Guns[i]
		local model = self.Models[i]
		local height = 10
		if(gunt == "bigbomb") then 
		local vehiclespawn = ents.Create("bigbomb")
		vehiclespawn:SetPos(self.Entity:GetPos()+Vector(0,0,height))
		vehiclespawn:SetNWEntity("owner", self.Entity:GetNWEntity("owner"))
		vehiclespawn:Spawn()
		vehiclespawn:Activate()
		else
		local weapon = ents.Create("spawned_weapon")
		weapon:SetPos(self.Entity:GetPos()+Vector(0,0,height))
		weapon:SetModel(model)
		weapon:GetTable().weaponclass = gunt
		weapon:Spawn()
		end
	end
	self.Guns = {}
	self.Models = {}
	if (ply!=nil) then
		if (ply:IsPlayer() && ValidEntity(ply)) then
			umsg.Start("killgunrecyclergui", ply)
				umsg.Short(self.Entity:EntIndex())
			umsg.End()
		end
	end
end

function ENT:Touch(gun)
	if (gun:GetClass()=="bigbomb" && table.Count(self.Guns)<20 && self.Ready == true && gun.AlreadyIn == nil && gun:GetTime()<CurTime()-30) then
		if(gun.Armed == true) then return end
		local wepclass = gun:GetClass()
		local model	   = gun:GetModel()
		if(Recycleables[wepclass] == nil) then return end
		gun.AlreadyIn = "notnill"
		table.insert(self.Guns, wepclass)
		table.insert(self.Models, model)
		gun:Remove()	
		return
	end
	if (gun:GetClass()=="spawned_weapon" && gun:GetTable().weaponclass !="weapon_physgun" && table.Count(self.Guns)<20 && self.Ready == true && gun:GetTime()<CurTime()-5) then
		if(gun.Armed == true) then return end
		local wepclass = gun:GetTable().weaponclass
		local model	   = gun:GetModel()
		if(Recycleables[wepclass] == nil) then return end
		table.insert(self.Guns, wepclass)
		table.insert(self.Models, model)
		// its a damn shame that the weapon doesnt actually remove instantly.
		gun:GetTable().weaponclass = "weapon_physgun"
		gun:Remove()	
	end
end

function ENT:StartProduction(ply,guntype)
	time = 300
	self.Timelefts = time
	self.User = ply
	if guntype==0 then
		self.Entity:SetNWBool("sparking",false)
		timer.Destroy(tostring(self.Entity) .. "getmoney")
		timer.Destroy(tostring(self.Entity) .. "dostuff")
		self.Ready = true
		self.GunType = nil
		Notify(ply, 2, 3, "Gun recyling stopped.")
	else
		self.Entity:SetNWBool("sparking",true)
		timer.Create(tostring(self.Entity) .. "getmoney", time, 1, self.giveMoney, self)
		timer.Create(tostring(self.Entity) .. "dostuff", 2, time/2, self.DoStuff, self)
		self.Ready = false
		self.GunType = guntype
		Notify(ply, 2, 3, "Recycling ".. table.Count(self.Guns) .." guns!")
	end
	umsg.Start("killgunrecyclergui", ply)
		umsg.Short(self.Entity:EntIndex())
	umsg.End()
end

function ENT:DoStuff()
	local owner = self.User
	self.Timelefts = self.Timelefts - 2
	if !self.Entity:IsPowered() then
		Notify(owner, 4, 3, "Failed to recycle weapons. Low power")
		self.Entity:SetNWBool("sparking",false)
		timer.Destroy(tostring(self.Entity) .. "dostuff")
		timer.Destroy(tostring(self.Entity) .. "getmoney")
		self.Ready = true
		return
	end
end

function ENT:CanProduce(guntype, ply)
	if guntype==0 && ply==self.Entity:GetNWEntity("owner") && self.Ready == false then
		return true
	elseif table.Count(self.Guns) == 0 then
		return false
	elseif self.Entity:IsPowered() == true && self.Ready == true && (guntype==1 || guntype==2) then
		return true
	else 
		return false
	end
end

function ENT:giveMoney()
	self.Entity:SetNWBool("sparking",false)
	self.Ready = true
	local money = 0
	local price = 0
	local ammount = 0
	for i=1, table.Count(self.Guns), 1 do
		gunt = self.Guns[i]
		if(Recycleables[gunt][2] == "shipment") then
			price = Recycleables[gunt][1] / 11 * 0.50
		end
		if(Recycleables[gunt][2] == "gunfactory") then
			price = Recycleables[gunt][1] * 0.50
		end
		if(Recycleables[gunt][2] == "pistol") then
			price = Recycleables[gunt][1] * 0.50
		end
		money = math.ceil(price + money)
	end
		local moneybag = ents.Create("prop_physics")
		moneybag:SetModel("models/props/cs_assault/money.mdl")
		moneybag:SetPos(self.Entity:GetPos() + Vector(0,0,25) )
		moneybag:SetAngles(self.Entity:GetAngles())
		moneybag:Spawn()
		moneybag:SetColor(200,255,200,255)
		moneybag:GetTable().MoneyBag = true
		moneybag:GetTable().Amount = money
	self.Guns = {}
	self.Models = {}
	Notify(self.User,3,3,"Gun recycler completed. Produced $" .. money)
end

function ENT:OnRemove()
	timer.Destroy(tostring(self.Entity) .. "dostuff")
	timer.Destroy(tostring(self.Entity) .. "getmoney")
	self.Entity:DropAllGuns(self.Entity:GetNWEntity("owner"))
end

function ccRecycleWeapon(ply, cmd, args)
	if (args[1]==nil || args[2] == nil) then return end
	guntype = tonumber(args[2])
	if !ValidEntity(ents.GetByIndex(args[1])) then return end
	if(guntype ~= 0 && guntype ~= 1 && guntype ~= 2) then return end	
	local vault = ents.GetByIndex(args[1])
	if (vault:GetClass()!="gunrecycler" || ply:GetPos():Distance(vault:GetPos())>300) then return end
	if (guntype == 2) then 
		vault:DropAllGuns() 
		umsg.Start("killgunrecyclergui", ply)
		umsg.Short(args[1])
		umsg.End()
		return 
	end
	if (vault:CanProduce(guntype, ply)) then
		vault:StartProduction(ply,guntype)
	else
		if guntype==0 then
			Notify(ply,4,3, "Only the owner can cancel weapon recyling.")
		else
			Notify(ply,4,3, "Cant recycle.")
		end
		umsg.Start("killgunrecyclergui", ply)
			umsg.Short(args[1])
		umsg.End()
	end	
end
concommand.Add("processgun", ccRecycleWeapon)